package com.hutianao.gqgl.log.control;

import com.hutianao.gqgl.log.service.LogService;
import com.hutianao.gqgl.mybatis.entity.LogEntity;
import com.hutianao.gqgl.mybatis.flexigrid.PaddingEntity;
import com.hutianao.gqgl.mybatis.flexigrid.PadingRstType;
import com.hutianao.gqgl.spring.control.BaseController;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.jdbc.UncategorizedSQLException;
import org.springframework.security.access.annotation.Secured;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import java.util.List;


@Controller
@RequestMapping("/log")
public  class LogController extends BaseController {
    private Logger logger = LogManager.getLogger(this.getClass());
    @Resource
    private LogService logService;
    @RequestMapping("/queryUserInfoListByName")
    @ResponseBody
    public List<LogEntity> queryUserInfoListByName(String  name) {
        logger.info(name);
        List<LogEntity> list = logService.querylogListByName(name);
        return list;
    }
    @Secured("ROLE_gqgy_data_log_delete")
    @RequestMapping("/deletelogInfoById")
    @ResponseBody
    public String deletelogInfoById(@RequestParam(value = "id",required = false) String id) {
        logger.info("ids:" + id);
        try {
            logService.deletelogInfoById(id);
        }catch (UncategorizedSQLException e){
            e.printStackTrace();
            return jsonFail("under.del.user.fail");
        }
        return jsonSuccess("user.del.user.success");
    }

    @Secured("ROLE_gqgy_data_log")
    @RequestMapping("/logPage")
    public String userPage(){
        return "log/logPage";
    }



    @RequestMapping("/logPageadd")
    public String logPageadd(){
        return "log/logPageadd";
    }



    @Secured("ROLE_gqgy_data_log_query")
    @RequestMapping("/querylogInfoListByPage")
    @ResponseBody
    public PadingRstType<LogEntity> querylogInfoListByPage(LogEntity entity, PaddingEntity paddingEntity){
        PadingRstType<LogEntity>  padingRstType= logService.querylogInfoListByPage(entity,paddingEntity);
        return padingRstType;
    }

    @RequestMapping("/getContext")
    @ResponseBody
    public PadingRstType<LogEntity> getContext(HttpServletRequest request){
        ServletContext application = request.getServletContext();
        return (PadingRstType<LogEntity>)application.getAttribute("logEntityPadingRstType");
    }
    @RequestMapping("/403")
    public String show403() {
        return "/error/403";
    }
}
